﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyThietBi.DAL.Models;

namespace QuanLyThietBi.DAL
{
    public class ThietBiDAO
    {
        /// <summary>
        /// Luu Thiet Bi vào database
        /// </summary>
        /// <param name="tb"></param>
        /// <returns></returns>
        public int SaveThietBi(THIETBI tb)
        {
            try
            {
                using (var en = new QuanLyThietBiEntities())
                {
                    en.THIETBIs.AddObject(tb);
                    return en.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

        }

        public int UpdateThietBi(THIETBI tb)
        {
            try
            {
                using (var en = new QuanLyThietBiEntities())
                {
                    //Lấy lên cái thiết bị dưới database
                    var tbupdate = en.THIETBIs.First(t => t.id == tb.id);

                    //update thong tin mới
                    tbupdate.loai_thiet_bi = tb.loai_thiet_bi;
                    tbupdate.ten_thiet_bi = tb.ten_thiet_bi;
                    tbupdate.ma_thiet_bi = tb.ma_thiet_bi;
                    tbupdate.tinh_trang = tb.tinh_trang;
                    tbupdate.mo_ta = tb.mo_ta;
                    tbupdate.hang_san_xuat = tb.hang_san_xuat;
                    tbupdate.nam_mua = tb.nam_mua;
                    //tbupdate.da_xoa = 1;

                    return en.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int XoaThietBi(THIETBI tb)
        {
            try
            {
                using (var en = new QuanLyThietBiEntities())
                {
                    //Lấy lên cái thiết bị dưới database
                    var tbupdate = en.THIETBIs.First(t => t.id == tb.id);

                    //update thong tin mới
                    //tbupdate.loai_thiet_bi = tb.loai_thiet_bi;
                    //tbupdate.ma_thiet_bi = tb.ma_thiet_bi;
                    //tbupdate.tinh_trang = tb.tinh_trang;
                    tbupdate.da_xoa = 1;

                    return en.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int Delete(THIETBI tb)
        {
            try
            {
                using (var en = new QuanLyThietBiEntities())
                {
                    //Lấy lên cái thiết bị dưới database
                    var tbDelete = en.THIETBIs.First(t => t.id == tb.id);
                    en.DeleteObject(tbDelete);
                    return en.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

        }


        /// <summary>
        /// Lấy Hết Thiết bị
        /// </summary>
        /// <returns></returns>
        public List<THIETBI> GetAllThietBi()
        {
            List<THIETBI> dsThietBi = null;
            try
            {
                using (var en = new QuanLyThietBiEntities())
                {
                    var ds = from tb in en.THIETBIs
                             where tb.da_xoa != 1
                             select tb;
                    if (ds != null)
                    {
                        dsThietBi = ds.ToList();
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return dsThietBi;
        }

        public int CountThietBi()
        {
            int count = 0;
            try
            {
                using (var en = new QuanLyThietBiEntities())
                {
                    var ds = from tb in en.THIETBIs
                             select tb;
                    if (ds != null)
                    {
                        count = ds.ToList().Count();
                    }
                }
            }
            catch { }

            return count;
        }

        public List<THIETBI> GetThietBiForSearch(string tenthietbi, string mathietbi)
        {
            List<THIETBI> dsThietBi = null;
            try
            {
                using (var en = new QuanLyThietBiEntities())
                {
                    var ds = from tb in en.THIETBIs
                             where tb.da_xoa != 1 && tb.ten_thiet_bi.Contains(tenthietbi) && tb.ma_thiet_bi.Contains(mathietbi) //&& tb.tinh_trang.Contains(tinhtrang)
                             select tb;
                    if (ds != null)
                    {
                        dsThietBi = ds.ToList();
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return dsThietBi;
        }

        //lấy ra 1 thiết bị theo thiết bị ID
        public THIETBI GetThietBiById(int thietBiId)
        {
            THIETBI thietBi = null;
            try
            {
                using (var en = new QuanLyThietBiEntities())
                {
                    var ds = from tb in en.THIETBIs
                             where tb.id == thietBiId
                             select tb;
                    if (ds != null)
                    {
                        thietBi = ds.FirstOrDefault();
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return thietBi;
        }

        public List<THIETBI> GetThietBiByLoaiThietBi(int loaiThietBiId)
        {
            List<THIETBI> dsThietBi = null;
            try
            {
                using (var en = new QuanLyThietBiEntities())
                {
                    var ds = from tb in en.THIETBIs
                             where tb.loai_thiet_bi == loaiThietBiId
                             select tb;
                    if (ds != null)
                    {
                        dsThietBi = ds.ToList();
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return dsThietBi;
        }


        public List<THIETBI> GetThietBiChuaSuDung(int loaiThietBiId)
        {
            List<THIETBI> dsThietBi = null;
            try
            {
                using (var en = new QuanLyThietBiEntities())
                {
                    var ds = from tb in en.THIETBIs
                             where tb.loai_thiet_bi == loaiThietBiId && tb.tinh_trang == 1
                             select tb;
                    if (ds != null)
                    {
                        dsThietBi = ds.ToList();
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return dsThietBi;
        }

        public List<THIETBI> GetMaThietBiForSearch(string mathietbi)
        {
            List<THIETBI> dsMaThietBi = null;
            try
            {
                using (var en = new QuanLyThietBiEntities())
                {
                    var ds = from tb in en.THIETBIs
                             where tb.da_xoa != 1 && tb.ma_thiet_bi.Contains(mathietbi)
                             select tb;
                    if (ds != null)
                    {
                        dsMaThietBi = ds.ToList();
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return dsMaThietBi;
        }

        public THIETBI GetMaThietBi(int thietbiId)
        {
            THIETBI mathietBi = null;
            try
            {
                using (var en = new QuanLyThietBiEntities())
                {
                    var ds = from tb in en.THIETBIs
                             where tb.id == thietbiId
                             select tb;
                    if (ds != null)
                    {
                        mathietBi = ds.FirstOrDefault();
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return mathietBi;
        }
    }
}
